package cn.gogoyc.org.service.impl;

import cn.gogoyc.org.basic.dto.ShopDto;
import cn.gogoyc.org.basic.exception.BusinessException;
import cn.gogoyc.org.basic.servie.impl.BaseServiceImpl;
import cn.gogoyc.org.basic.util.BaiduAuditUtils;
import cn.gogoyc.org.basic.util.Md5Utils;
import cn.gogoyc.org.basic.util.StrUtils;
import cn.gogoyc.org.domain.Employee;
import cn.gogoyc.org.domain.Shop;
import cn.gogoyc.org.domain.ShopAuditLog;
import cn.gogoyc.org.mapper.EmployeeMapper;
import cn.gogoyc.org.mapper.ShopAuditLogMapper;
import cn.gogoyc.org.mapper.ShopMapper;
import cn.gogoyc.org.service.ShopService;
import cn.gogoyc.user.domain.Logininfo;
import cn.gogoyc.user.mapper.LogininfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * 店铺业务实现类
 *
 * @author L1vezZ
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements ShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private LogininfoMapper logininfoMapper;

    /**
     * 店铺注册入驻
     * @param shop
     */
    @Override
    public void settlement(Shop shop) {
        //一.校验
        //1.空值校验
        if (StringUtils.isEmpty(shop.getName()) || StringUtils.isEmpty(shop.getTel())) {
            throw new BusinessException("信息不能为空!");
        }
        //2.两次密码不一致体验
        if (!shop.getAdmin().getPassword().equals(shop.getAdmin().getComfirmPassword())) {
            throw new BusinessException("两次密码不一致!");
        }
        //3.当前店铺是否入驻过
        Shop dbShop = shopMapper.findByNameAndAddress(shop);
        if (dbShop != null) {
            throw new BusinessException("当前店铺已经入驻过,请直接登录!");
        }
        //二.百度AI智能审核【店铺名,Login是否合法】
        if (!BaiduAuditUtils.TextCensor(shop.getName())) {
            throw new BusinessException("店铺名称不合格!!!");
        }
        if (!BaiduAuditUtils.ImgCensor("http://123.207.27.208" + shop.getLogo())) {
            throw new BusinessException("店铺Logo不合格!!!");
        }

        //三.添加到数据库
        //1.去获取shop对象中的Emp员工对象。
        Employee admin = shop.getAdmin();
        //加盐加密
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(admin.getPassword() + salt);
        admin.setSalt(salt);
        admin.setPassword(md5Pwd);

        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(admin,logininfo);
        logininfo.setType(0);
        logininfo.setDisable(true);
        logininfoMapper.save(logininfo);

        admin.setLogininfo_id(logininfo.getId());
        //这里添加两次的原因是,因为下面店铺关联员工需要自增ID
        employeeMapper.save(admin);

        //2.再添加店铺信息
        //获取员工的Id并且将该值设置进店铺的店长Admin_id 关联起来
        shop.setAdminId(admin.getId());
        //将关联好的对象添加进店铺表
        shopMapper.save(shop);

        //3.再添加员工信息
        // 获取店铺ID将该值设置进员工表的Shop_id店铺ID 关联起来
        admin.setShop_id(shop.getId());
        //将关联好的对象添加进店铺表
        employeeMapper.update(admin);
    }

    /**
     * 邮箱驳回事件
     * @param log
     * @throws MessagingException
     */
    @Override
    public void auditReject(ShopAuditLog log, Logininfo logininfo) throws MessagingException {
        //修改关联信息 根据ShopId关联去修改店铺的状态
        shopMapper.updateState(4, log.getShopId());
        //修改后日志也要修改值同步信息
        log.setState(4);

        log.setAuditId(logininfo.getId());
        //将修改后的值重新设置进日志表
        shopAuditLogMapper.save(log);


        //构建信息
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        //设置发送方邮箱号
        helper.setFrom("1462058295@qq.com");
        //设置邮箱标题
        helper.setSubject("店铺激活邮件");
        //获取当前日志行对象的shopId查出对应的店铺对象信息
        Shop shop = shopMapper.loadById(log.getShopId());
        //true - 能够再内容中编写html标签 - 会解析
        helper.setText("<h3>你的店铺审核未通过，请<a href='http://localhost:8081/#/register'>点击这里</a>修改入驻信息重新提交</h3>", true);
        //店铺管理员 这里是根据日志Shop_id查到对应的店铺信息,再通过店铺的AdminId去查到对应的店长对象
        Employee admin = employeeMapper.loadById(shop.getAdminId());
        //根据对应的店长邮箱发送邮件
        helper.setTo(admin.getEmail());
        //发送
        javaMailSender.send(mimeMessage);
    }

    /**
     * 邮箱激活并同步日志
     * @param log
     * @throws MessagingException
     */
    @Override
    public void auditPass(ShopAuditLog log,Logininfo logininfo) throws MessagingException {
        //修改关联信息 根据ShopId关联去修改店铺的状态
        shopMapper.updateState(2, log.getShopId());
        //日志,记录状态信息
        log.setState(2);
        //伪代码 写死
        log.setAuditId(logininfo.getId());
        //将当前日志添加到数据库
        shopAuditLogMapper.save(log);


        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("1462058295@qq.com");
        helper.setSubject("店铺激活邮件");
        //根据关联信息Shop_Id查到对应的店铺对象 目的:三连查 日志->店铺->店铺店长 找到对应的店长邮箱
        Shop shop = shopMapper.loadById(log.getShopId());
        //true - 能够再内容中编写html标签 - 会解析
        helper.setText("<h3>你的店铺已经审核通过，请<a href='http://172.16.6.225:8080/shop/active/" + log.getShopId()
                + "'>点击这里</a>激活邮件</h3>", true);
        //根据日志对应的店铺信息AdminId找到对应的店长信息对象
        Employee admin = employeeMapper.loadById(shop.getAdminId());
        //获取到对应店长对象后,将他的邮箱设置进去
        helper.setTo(admin.getEmail());
        //发送
        javaMailSender.send(mimeMessage);
    }

    /**
     * 邮箱激活修改状态
     * @param id
     */
    @Override
    public void activatedState(Long id) {
        shopMapper.updateState(3, id);
    }

    /**
     * 报表展示
     * @return
     */
    @Override
    public List<ShopDto> getNumAndState() {
        return shopMapper.getNumAndState();
    }
}
